import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import java.util.*;
public class EmployeeDetail extends Frame implements ActionListener{
    Frame f;
    Panel p;
    Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15;
    Label l1,l2,l3,l4,l6,l7,l8,l5,l9,l10;
    TextField t1,t2,t3,t4,t5,t6,t7,t8;
    Statement st;
    Connection con;
    ResultSet rs;
    public EmployeeDetail()
    {
        
        try{
            setLayout(null);
            setBackground(Color.CYAN);
            f=new Frame("Mini Project Details");
            Font fon=new Font("sylfacn",Font.BOLD,14);
            setFont(fon);
            p=new Panel();
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con=DriverManager.getConnection("jdbc:odbc:mini","system","system");
            st=con.createStatement();
           
        }
        catch(Exception e){
            System.out.println(e);

        }
    }
       
        public void createpanel()
    {        
        p.setBackground(Color.lightGray);
        p.setBounds(100,50,300,30);
        add(p);
        l1=new Label("Employee Maintanace");
        l1.setBounds(50,100,200,25);
        p.add(l1);
    }
    public void buttonpanel()
    {
        Panel bp=new Panel();
        bp.setBackground(Color.cyan);
        bp.setBounds(50,200,400,35);
        add(bp);
        b1=new Button("Sign in");
        //b1.setBounds(50,300,50,35);
        b1.addActionListener(this);
        bp.add(b1);
        b2=new Button("Reset");
       // b2.setBounds(50,350,50,35);
        b2.addActionListener(this);
        bp.add(b2);
        
    }
    public void components()
    {
        l1 =new Label("User Name");
        l1.setBounds(150,100,150,20);
        add(l1);
        l2 =new Label("Password");
        l2.setBounds(150,140,150,20);
        add(l2);
        t1=new TextField(10);
        t1.setBounds(300,100,80,20);
        add(t1);
        t2=new TextField(10);
        t2.setBounds(300,140,80,20);
        add(t2);
        t2.setEchoChar('*');
    
    }
        public void mainpage()
    {
        
        b3=new Button("Department page");
        b3.setBounds(100,70,150,20);
        b3.addActionListener(this);
        add(b3);
        b4=new Button("Employee page");
        b4.setBounds(200,140,150,20);
        b4.addActionListener(this);
        add(b4);
        b5=new Button("exit");
        b5.setBounds(300,210,150,20);
        b5.addActionListener(this);
        add(b5);

    }
    public void deptpage()
    {
        l9=new Label("DEPARTMENT PAGE");
        l9.setBounds(200,50,400,20);
        add(l9);
        l3 =new Label("Dept number");
        l3.setBounds(50,100,100,20);
        add(l3);
        l4=new Label("dept name");
        l4.setBounds(50,140,100,20);
        add(l4);
        t3=new TextField(10);
        t3.setBounds(250,100,200,20);
        add(t3);
        t4=new TextField(10);
        t4.setBounds(250,140,200,20);
        add(t4);
        b6=new Button("insert");
        b6.setBounds(250,210,150,20);
        b6.addActionListener(this);
        add(b6);
        b7=new Button("update");
        b7.setBounds(420,210,150,20);
        b7.addActionListener(this);
        add(b7);
        b8=new Button("delete");
        b8.setBounds(250,280,150,20);
        b8.addActionListener(this);
        add(b8);
        b9=new Button("select");
        b9.setBounds(420,280,150,20);
        b9.addActionListener(this);
        add(b9);
        b10=new Button("Exit");
        b10.setBounds(350,350,150,20);
        b10.addActionListener(this);
        add(b10);
    }
    public void emppage()
    {
        l10=new Label("EMPLOYEE PAGE");
        l10.setBounds(500,50,800,20);
        add(l10);
        l5 =new Label("Employee number");
        l5.setBounds(50,100,140,20);
        add(l5);
        t5=new TextField(10);
        t5.setBounds(250,100,200,20);
        add(t5);
        l6=new Label("employee name");
        l6.setBounds(50,140,140,20);
        add(l6);
        t6=new TextField(10);
        t6.setBounds(250,140,200,20);
        add(t6);
        l7 =new Label("salary");
        l7.setBounds(650,100,100,20);
        add(l7);
        t7=new TextField(10);
        t7.setBounds(750,100,200,20);
        add(t7);
        l8=new Label("dept number");
        l8.setBounds(650,140,100,20);
        add(l8);
        t8=new TextField(10);
        t8.setBounds(750,140,200,20);
        add(t8);
        b11=new Button("insert");
        b11.setBounds(250,210,150,20);
        b11.addActionListener(this);
        add(b11);
        b12=new Button("update");
        b12.setBounds(420,210,150,20);
        b12.addActionListener(this);
        add(b12);
        b13=new Button("delete");
        b13.setBounds(250,280,150,20);
        b13.addActionListener(this);
        add(b13);
        b14=new Button("select");
        b14.setBounds(420,280,150,20);
        b14.addActionListener(this);
        add(b14);
        b15=new Button("Exit");
        b15.setBounds(350,350,150,20);
        b15.addActionListener(this);
        add(b15);
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==b1)
        {
            login();
        }
        else if(e.getSource()==b2)
        {
            t1.setText(" ");
            t2.setText(" ");
        }
        else if(e.getSource()==b3)//to select department page 
        {
            removeAll();
            deptpage();
        }
        else if(e.getSource()==b4)//to select employee page
        {
            removeAll();
            emppage();
        }else if(e.getSource()==b5)
        {
            removeAll();
            System.exit(0);
        }
        else if(e.getSource()==b6)
        {
            insert1();//insering function in the "dept" table 
        }
        else if(e.getSource()==b7)
        {
            update1();//updating function in the "dept" table 
        }
        else if(e.getSource()==b8)
        {
            delete1();//deleting function  in the "dept" table 
        }
        else if(e.getSource()==b9)
        {
            select1();//selecting function in the "dept" table 
        }
        else if(e.getSource()==b10)//Exit from the "dept" table to the main page
        {
            removeAll();
            mainpage();
        }
        else if(e.getSource()==b11)//inserting data in the "emp" page
        {
            insert2();
        }
        else if(e.getSource()==b12)//updating data in "emp" page
        {
            update2();
        }
        else if(e.getSource()==b13)//deleting data from the "emp" page
        {
            delete2();
        }
        else if(e.getSource()==b14)//select data from the "emp" page
        {
            select2();
        }
        else if(e.getSource()==b15)
        {
            removeAll();
            mainpage();
        }
        }
    public void insert1()
    {
        try{
            int dno=Integer.parseInt(t3.getText());
            String dname=t4.getText();
            String s="insert into dept values("+dno+",'"+dname+"')";
            st.executeUpdate(s);
            System.out.println("Records Inserted");
            t3.setText(" ");//setting the textfield as empty after inserting  data
            t4.setText(" ");//setting the textfield as empty after inserting  data
        }catch(Exception e)
        {
            System.out.println("Error in inerting a data"+e);
        }
    }
     public void update1()
    {
        try{
            int dno=Integer.parseInt(t3.getText());
            String dname=t4.getText();
            String s="update dept set dname='"+dname+"'where dno="+dno;
            st.executeUpdate(s);
            con.commit();
            System.out.println("records updated");
            t3.setText(" ");
            t4.setText(" ");
        }catch(Exception e)
        {
            System.out.println("Exception in updating "+e);
        }
    }
     public void delete1()
     {
         try{
             int dno=Integer.parseInt(t3.getText());
             st.executeUpdate("delete from dept where dno="+dno);
             con.commit();
             System.out.println("Records deleted");
             t3.setText(" ");
         }
         catch(Exception e)
         {
             System.out.println("Error in deleting"+e);
             
     }
     }
     public void select1()
     {
         try
         {
             int dno=Integer.parseInt(t3.getText());
             rs=st.executeQuery("select * from dept where dno="+dno);
             while(rs.next())
             {
                 System.out.println("dno : "+rs.getInt(1));
                 System.out.println("dname : "+rs.getString(2));
                 dno=rs.getInt(1);
                 String dname=rs.getString(2);
                 t4.setText(dname);
             }System.out.println("Successfully selected");
         }catch(Exception e)
         {
             System.out.println("Error in selecting"+e);
         }
     }
     public void insert2()
     {
         try
         {
            int eno=Integer.parseInt(t5.getText());
            String ename=t6.getText();
            int sal=Integer.parseInt(t7.getText());
            int depno=Integer.parseInt(t8.getText());
            String s="insert into emp values("+eno+",'"+ename+"',"+sal+","+depno+")";
            st.executeUpdate(s);
            System.out.println("Records Inserted");
            t5.setText(" ");
            t6.setText(" ");
            t7.setText(" ");
            t8.setText(" ");
         }catch(Exception e)
         {
             System.out.println("Exception in inserting"+e);
         }
     }
     public void update2()
    {
        try{
            int eno=Integer.parseInt(t5.getText());
            String ename=t6.getText();
            int sal=Integer.parseInt(t7.getText());
            int deptno=Integer.parseInt(t8.getText());
            String s="update emp set ename='"+ename+"',sal="+sal+",deptno="+deptno+" where eno="+eno;
            st.executeUpdate(s);
            con.commit();
            System.out.println("records updated");
            t5.setText(" ");
            t6.setText(" ");
            t7.setText(" ");
            t8.setText(" ");
        }catch(Exception e)
        {
            System.out.println("Exception in updating "+e);
        }
    }

     public void delete2()
     {
         try{
             int eno=Integer.parseInt(t5.getText());
             st.executeUpdate("delete from emp where eno="+eno);
             con.commit();             
             System.out.println("Records deleted");
             t5.setText(" ");
         }
         catch(Exception e)
         {
             System.out.println("Error in deleting"+e);
             
     }
     }
public void select2()
     {
         try
         {
             int eno=Integer.parseInt(t5.getText());
             rs=st.executeQuery("select * from emp where eno="+eno);
             while(rs.next())
             {
                 System.out.println("eno : "+rs.getInt(1));
                 System.out.println("ename : "+rs.getString(2));
                 System.out.println("sal : "+rs.getInt(3));
                 System.out.println("dno : "+rs.getInt(4));
                 
                 eno=rs.getInt(1);
                 String ename=rs.getString(2);
                // int sal=rs.getInt(3);
                 //int deptno=rs.getInt(4);
                 t6.setText(ename);
                 t7.setText(String.valueOf(rs.getInt(3)).toString());
                 t8.setText(String.valueOf(rs.getInt(4)).toString());
             }System.out.println("Successfully selected");
         }catch(Exception e)
         {
             System.out.println("Error in selecting"+e);
         }
     }
    public void login()
    {
        String  us="admin";
        String  pw="admin";
        String use=t1.getText();
        String pass=t2.getText();
        if(us.equals(use)&&pw.equals(pass))
        {
            removeAll();
            mainpage();
        }
    }
     
    public static void main(String args[])
    {
    EmployeeDetail ed=new EmployeeDetail();
    ed.createpanel();
    ed.buttonpanel();
    ed.components();
    ed.setSize(450,500);
    ed.setVisible(true);
    ed.addWindowListener(new WindowAdapter()
    {
     public void windowClosing(WindowEvent e)
     {
            System.exit(0);
        }
    });
   ed.show();
    }

}
